package cn.acac.yygh.hosp.listener;

import cn.acac.yygh.hosp.service.ScheduleService;
import cn.acac.yygh.mq.MqConst;
import cn.acac.yygh.mq.RabbitMqService;
import cn.acac.yygh.vo.msm.MsmVo;
import cn.acac.yygh.vo.order.OrderMqVo;
import com.rabbitmq.client.Channel;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderMqListener {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RabbitMqService rabbitMqService;

    @RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue(name = MqConst.QUEUE_ORDER,durable = "true"),
                exchange = @Exchange(name = MqConst.EXCHANGE_DIRECT_ORDER),
                key = MqConst.ROUTING_ORDER
        )
    })
    public void consumer(OrderMqVo orderMqVo, Message message, Channel channel){
        String scheduleId = orderMqVo.getScheduleId();      // 获取排班id
        Integer availableNumber = orderMqVo.getAvailableNumber();   // 获取可预约数量
        Boolean flag = null;
        MsmVo msmVo = null;
        if(availableNumber != null){
            // 确认订单
            flag = scheduleService.updateScheduleById(scheduleId,availableNumber);
            msmVo = orderMqVo.getMsmVo();

        }else{
            // 取消订单
            flag = scheduleService.cancelSchedule(scheduleId);
            msmVo = orderMqVo.getMsmVo();
        }
        if(flag){
            // 发送短信
            rabbitMqService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);
        }
    }
}
